package com.la.bc_api.utils;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Author LA
 * @Date 2024/8/29 16:51
 * @Description: Http工具
 */
public class httpClientUtil {
    private static final String apiKey = "sk-719d500bf5e76276f968b74bb859b155";

    /**
     * post请求
     * @param url
     * @param jsonBody
     * @return
     */
    public static CloseableHttpResponse doPost(String url,String jsonBody){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建POST请求
        HttpPost httpPost = new HttpPost(url);
        //设置请求头
        httpPost.setHeader("Content-Type","application/json");
        httpPost.setHeader("Authorization","Bearer "+apiKey);
        //将请求体设置为StringEntity
        StringEntity stringEntity = new StringEntity(jsonBody, StandardCharsets.UTF_8);
        httpPost.setEntity(stringEntity);
        //执行请求
        try {
            return httpClient.execute(httpPost);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
